package com.gitee.qdbp.general.system.biz.permission.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.jdbc.paging.Paging;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.model.consts.Keys;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.general.system.api.permission.basic.IResourceCoreBasic;
import com.gitee.qdbp.general.system.api.permission.basic.IRoleResourceRefBasic;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCascadeBean;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCoreBean;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCoreUpdate;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCoreWhere;
import com.gitee.qdbp.general.system.api.permission.model.RoleResourceRefBean;
import com.gitee.qdbp.general.system.api.permission.model.RoleResourceRefWhere;
import com.gitee.qdbp.general.system.api.permission.service.IResourceCascadeQueryer;
import com.gitee.qdbp.general.system.api.permission.service.IResourceCoreExecutor;
import com.gitee.qdbp.general.system.enums.SysResState;
import com.gitee.qdbp.general.system.enums.SysResType;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 资源信息业务处理类
 *
 * @author zhh
 * @version 170215
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class ResourceCoreExecutor implements IResourceCoreExecutor {

    /** 日志对象 **/
    private static final Logger log = LoggerFactory.getLogger(ResourceCoreExecutor.class);

    @Autowired
    private IResourceCoreBasic resourceCoreBasic;
    @Autowired
    private IResourceCascadeQueryer resourceCascadeQueryer;
    @Autowired
    private IRoleResourceRefBasic roleResourceRefBasic;

    @Override
    public void swapIndex(String scene, String oneId, String twoId, IAccount me) throws ServiceException {
        String msg = "Failed to swap ResourceIndex. ";
        if (VerifyTools.isBlank(scene)) {
            log.warn(msg + "params is null: SceneType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        ResourceCoreBean one = findByCodeThrowOnNotExists(oneId, msg);
        ResourceCoreBean two = findByCodeThrowOnNotExists(twoId, msg);

        String parentId = one.getParentId();
        if (!parentId.equals(two.getParentId())) {
            log.error(msg + "parent id not equals, id={},{}", oneId, twoId);
            throw new ServiceException(ResultCode.PARAMETER_VALUE_ERROR);
        }
        if (one.getSortIndex().equals(two.getSortIndex())) {
            int index = one.getSortIndex();
            { // 大于等于当前序号的全部加1
                ResourceCoreUpdate ud = new ResourceCoreUpdate();
                ud.setSortIndexAdd(1);
                ResourceCoreWhere where = ud.getWhere(true);
                where.setParentId(parentId);
                where.setSortIndexMin(index);
                resourceCoreBasic.update(ud, false);
            }
            { // one再改成之前的序号
                ResourceCoreUpdate ud = new ResourceCoreUpdate();
                ud.setSortIndex(index);
                ResourceCoreWhere where = ud.getWhere(true);
                where.setParentId(parentId);
                where.setId(one.getId());
                resourceCoreBasic.update(ud, false);
            }
        } else {
            { // one的序号改成two的序号
                ResourceCoreUpdate ud = new ResourceCoreUpdate();
                ud.setSortIndex(two.getSortIndex());
                ResourceCoreWhere where = ud.getWhere(true);
                where.setParentId(parentId);
                where.setId(one.getId());
                resourceCoreBasic.update(ud, false);
            }
            { // two的序号改成one的序号
                ResourceCoreUpdate ud = new ResourceCoreUpdate();
                ud.setSortIndex(one.getSortIndex());
                ResourceCoreWhere where = ud.getWhere(true);
                where.setParentId(parentId);
                where.setId(two.getId());
                where.setSortIndex(two.getSortIndex());
                resourceCoreBasic.update(ud, false);
            }
        }

    }

    @Override
    public void updateIndex(String scene, String id, int sortIndex, IAccount me) throws ServiceException {
        String msg = "Failed to update ResourceIndex. ";
        if (VerifyTools.isBlank(id)) {
            log.error(msg + "params is null: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(scene)) {
            log.warn(msg + "params is null: SceneType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        ResourceCoreBean older = resourceCoreBasic.findById(id);

        if (VerifyTools.equals(sortIndex, older.getSortIndex())) {
            return;
        }

        doUpdateIndex(older, sortIndex, true, me);
    }

    private void doUpdateIndex(ResourceCoreBean older, int sortIndex, boolean moveSelf, IAccount me)
            throws ServiceException {
        if (sortIndex > older.getSortIndex()) { // 向后移
            // 先将中间的记录向前移
            // 如: 原先是3, 想移动到6, 则先将456向前移, 变成345, 再将原先的3变成6
            ResourceCoreUpdate ud = new ResourceCoreUpdate();
            ud.setSortIndexAdd(-1);
            ResourceCoreWhere where = ud.getWhere(true);
            where.setSortIndexMin(older.getSortIndex() + 1);
            where.setSortIndexMax(sortIndex);
            where.setParentId(older.getParentId());
            resourceCoreBasic.update(ud, false);
        } else { // 向前移
            // 先将中间的记录向后移
            // 如: 原先是6, 想移动到3, 则先将345向后移, 变成456, 再将原先的6变成3
            ResourceCoreUpdate ud = new ResourceCoreUpdate();
            ud.setSortIndexAdd(+1);
            ResourceCoreWhere where = ud.getWhere(true);
            where.setSortIndexMin(sortIndex);
            where.setSortIndexMax(older.getSortIndex() - 1);
            where.setParentId(older.getParentId());
            resourceCoreBasic.update(ud, false);
        }
        if (moveSelf) { // 再将自已移动到指定位置
            ResourceCoreUpdate ud = new ResourceCoreUpdate();
            ud.setSortIndex(sortIndex);
            ResourceCoreWhere where = ud.getWhere(true);
            where.setId(older.getId());
            resourceCoreBasic.update(ud, true);
        }
    }

    @Override
    public void create(ResourceCascadeBean model, IAccount me) throws ServiceException {
        
        create((ResourceCoreBean) model, me);

        String id = model.getId();

        // 子权限列表
        List<ResourceCascadeBean> children = model.getChildren();

        // 判断菜单是否存在查询权限
        // 菜单如果没有查询操作权限是玩不下去的, 需要自动加上查询权限
        if (model.getType() == SysResType.MENU) {
            if (children == null) {
                children = new ArrayList<>();
            }

            ResourceCascadeBean q = null;
            String qkey = model.getKey() + ":query";
            for (ResourceCascadeBean operate : children) {
                if (qkey.equals(operate.getKey())) {
                    q = operate;
                    break;
                }
            }

            if (q == null) { // 自动加上查询权限
                q = new ResourceCascadeBean();
                q.setKey(qkey);
                q.setName("查询");
                q.setSortIndex(1);
                children.add(q);
            }
        }

        if (VerifyTools.isNotBlank(children)) {
            // 将子权限保存到数据库
            for (ResourceCascadeBean child : children) {
                child.setType(SysResType.OPERATE);
                child.setSceneType(model.getSceneType());
                child.setParentId(id);
                create(child, me);
            }
        }
    }

    private void create(ResourceCoreBean model, IAccount me) throws ServiceException {
        String msg = "Failed to create ResourceCore. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model.getSceneType())) {
            log.warn(msg + "params is null: SceneType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 参数验证
        if (VerifyTools.isBlank(model.getType())) {
            log.error(msg + "params is null: type");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model.getKey())) {
            log.error(msg + "params is null: key");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model.getName())) {
            log.error(msg + "params is null: name");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        String parentId = model.getParentId();
        if (VerifyTools.isBlank(parentId)) {
            model.setParentId(parentId = Keys.ROOT_CODE);
        }
        if (model.getState() == null) {
            model.setState(SysResState.NORMAL);
        }

        // 如果没有填sortIndex, 自动排到最后
        if (VerifyTools.isBlank(model.getSortIndex())) {
            ResourceCoreWhere where = new ResourceCoreWhere();
            where.setParentId(parentId);
            OrderPaging p = OrderPaging.of(new Paging(1, 1, true), "sortIndex desc"); // 降序取第1条
            PageList<ResourceCoreBean> temp = resourceCoreBasic.list(where, p);
            int sortIndex = 1;
            if (VerifyTools.isNotBlank(temp)) {
                ResourceCoreBean max = temp.get(0);
                sortIndex = Math.max(VerifyTools.nvl(max.getSortIndex(), 0) + 1, temp.getTotal());
            }
            model.setSortIndex(sortIndex);
        } else { // 如果填了sortIndex, 将现在的sortIndex向后移, 为新记录腾出空位
            ResourceCoreUpdate ud = new ResourceCoreUpdate();
            ud.setSortIndexAdd(1);
            ResourceCoreWhere w = ud.getWhere(true);
            w.setParentId(parentId);
            w.setSortIndexMin(model.getSortIndex());
            resourceCoreBasic.update(ud, false);
        }

        // 向sys_resource_core_info表插入记录
        resourceCoreBasic.create(model);
    }

    @Override
    public void update(ResourceCoreUpdate model, IAccount me) throws ServiceException {
        String msg = "Failed to update ResourceCore. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(model.getSceneType())) {
            log.warn(msg + "params is null: SceneType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        String id = model.getId();
        if (VerifyTools.isBlank(id) && model.getWhere() != null) {
            id = model.getWhere().getId();
        }
        String key = model.getKey();
        if (VerifyTools.isBlank(key) && model.getWhere() != null) {
            key = model.getWhere().getKey();
        }
        if (VerifyTools.isAllBlank(id, key)) {
            log.error(msg + "id or key is required.");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 原始记录检查
        ResourceCoreBean older = resourceCoreBasic.findById(id);
        if (older == null) {
            log.error(msg + "record is not found. id=" + id);
            throw new ServiceException(ResultCode.RECORD_NOT_EXIST);
        }

        ResourceCoreUpdate newer = createChangedModel(model, older);

        if (newer != null) {
            if (VerifyTools.isNotBlank(newer.getSortIndex())) {
                doUpdateIndex(older, newer.getSortIndex(), false, me);
            }

            if (VerifyTools.isBlank(id)) {
                model.getWhere(true).setKey(key);
            }
            // 更新sys_resource_core_info表的记录
            resourceCoreBasic.update(newer, false);
        }
    }

    @Override
    public void delete(String scene, List<String> ids, boolean cascade, IAccount me) throws ServiceException {
        String msg = "Failed to delete ResourceCore. ";

        if (VerifyTools.isBlank(me)) {
            log.error(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(ids)) {
            log.error(msg + "params is null: ids");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(scene)) {
            log.warn(msg + "params is null: SceneType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        List<String> targets = cascade ? collectAllChildrenId(scene, ids) : ids;
        // 删除sys_resource_core_info表的记录
        ResourceCoreWhere where = new ResourceCoreWhere();
        where.setSceneType(scene);
        where.setIds(targets);
        resourceCoreBasic.delete(where, false);
    }

    @Override
    public void disable(String scene, List<String> ids, IAccount me) throws ServiceException {
        String msg = "Failed to disable ResourceCore. ";
        if (VerifyTools.isBlank(scene)) {
            log.warn(msg + "params is null: SceneType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 禁用, 同时禁用全部下级

        List<String> targets = collectAllChildrenId(scene, ids);

        ResourceCoreUpdate ud = new ResourceCoreUpdate();
        ud.setState(SysResState.DISABLED);
        ResourceCoreWhere where = ud.getWhere(true);
        where.setSceneType(scene);
        where.setIds(targets);
        resourceCoreBasic.update(ud, false);
    }

    @Override
    public void enable(String scene, List<String> ids, boolean cascade, IAccount me) throws ServiceException {
        String msg = "Failed to enable ResourceCore. ";
        if (VerifyTools.isBlank(scene)) {
            log.warn(msg + "params is null: SceneType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        List<String> targets = cascade ? collectAllChildrenId(scene, ids) : ids;

        ResourceCoreUpdate ud = new ResourceCoreUpdate();
        ud.setState(SysResState.NORMAL);
        ResourceCoreWhere where = ud.getWhere(true);
        where.setSceneType(scene);
        where.setIds(targets);
        resourceCoreBasic.update(ud, false);
    }

    @Override
    public void distributeResources(String roleId, String scene, List<String> resourceIds, IAccount me)
            throws ServiceException {
        String msg = "Failed to distribute resources. ";

        if (VerifyTools.isBlank(roleId)) {
            log.warn(msg + "params is null: roleId");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (resourceIds == null) { // resourceIds=null报错, resourceIds.size=0则清空所有权限
            log.warn(msg + "params is null: resourceIds");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(me)) {
            log.warn(msg + "params is null: me");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        if (VerifyTools.isBlank(scene)) {
            log.warn(msg + "params is null: SceneType");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 重新分配角色关系
        doDistributeResources(scene, roleId, resourceIds, me);
    }

    // role_resource_ref表记录的只有OPERATE这一级, 没有记模块/菜单组/菜单
    private void doDistributeResources(String scene, String roleId, List<String> resourceIds, IAccount me)
            throws ServiceException {

        if (resourceIds.isEmpty()) {
            // resourceIds.size=0则清空所有权限
            RoleResourceRefWhere where = new RoleResourceRefWhere();
            where.setSceneType(scene);
            where.setRoleId(roleId);
            roleResourceRefBasic.delete(where, false);
            return;
        }

        // 查询该用户原有的角色
        PageList<RoleResourceRefBean> old;
        {
            RoleResourceRefWhere where = new RoleResourceRefWhere();
            where.setSceneType(scene);
            where.setRoleId(roleId);
            old = roleResourceRefBasic.list(where, OrderPaging.NONE);
        }

        // 查找数据库不存在的, 就是需要增加的
        Set<String> needAddList = new HashSet<>();
        // 数据库比传入参数多的, 就是需要删除的
        Set<String> needDelList = new HashSet<>();

        Map<String, RoleResourceRefBean> map = new HashMap<>();
        if (VerifyTools.isNotBlank(old)) {
            for (RoleResourceRefBean i : old) {
                map.put(i.getResourceId(), i);
                if (resourceIds.indexOf(i.getResourceId()) < 0) {
                    needDelList.add(i.getResourceId());
                }
            }
        }

        for (String resourceId : resourceIds) {
            if (!map.containsKey(resourceId)) {
                needAddList.add(resourceId);
            }
        }

        // 创建需要增加的
        if (VerifyTools.isNotBlank(needAddList)) {
            int max = 20;
            List<RoleResourceRefBean> list = new ArrayList<>();
            for (String resourceId : needAddList) {
                RoleResourceRefBean model = new RoleResourceRefBean();
                model.setSceneType(scene);
                model.setRoleId(roleId);
                model.setResourceId(resourceId);
                list.add(model);
                if (list.size() >= max) {
                    roleResourceRefBasic.create(list);
                    list.clear();
                }
            }
            if (!list.isEmpty()) {
                roleResourceRefBasic.create(list);
            }
        }

        // 删除数据库多的
        if (VerifyTools.isNotBlank(needDelList)) {
            RoleResourceRefWhere where = new RoleResourceRefWhere();
            where.setSceneType(scene);
            where.setRoleId(roleId);
            where.setResourceIds(new ArrayList<>(needDelList));
            roleResourceRefBasic.delete(where, false);
        }
    }

    // 收集所有后代节点的ID
    private List<String> collectAllChildrenId(String scene, List<String> ids) throws ServiceException {
        VerifyTools.requireNotBlank(scene, "scene");

        PageList<ResourceCoreBean> all;
        {
            ResourceCoreWhere where = new ResourceCoreWhere();
            where.setSceneType(scene);
            all = resourceCoreBasic.list(OrderPaging.NONE);
        }

        ResourceCascadeBean root = resourceCascadeQueryer.wrap(all.toList(), true);
        Iterator<ResourceCascadeBean> rootIterator = root.breadthFirstIterator();

        List<String> targets = new ArrayList<>();
        targets.addAll(ids);

        // 收集所有后代节点的ID
        while (rootIterator.hasNext()) {
            ResourceCascadeBean item = rootIterator.next();
            if (targets.indexOf(item.getId()) >= 0) {
                continue; // 已在待删除列表中
            }
            if (targets.indexOf(item.getParentId()) >= 0) {
                targets.add(item.getId()); // 当前节点的父节点在待删除列表中, 当前节点也应加入
            }
        }
        return targets;
    }

    /**
     * 检查资源是否存在, 不存在则报错
     *
     * @param model 节点信息
     * @throws ServiceException
     */
    private ResourceCoreBean findByCodeThrowOnNotExists(String id, String msg) throws ServiceException {

        ResourceCoreWhere where = new ResourceCoreWhere();
        where.setId(id);

        ResourceCoreBean bean = resourceCoreBasic.find(where);

        if (bean == null) {
            log.warn(msg + "Resource not found, id=" + id);
            throw new ServiceException(ResultCode.RECORD_NOT_EXIST);
        }
        return bean;
    }

    /**
     * 创建一个新的对象, 只包含改动过的字段
     *
     * @author zhh
     * @param model 目标对象(一般是参数传入的)
     * @param older 源对象(一般是从数据库查询得到的)
     * @return 只包含有更新的字段对象
     */
    private ResourceCoreUpdate createChangedModel(ResourceCoreUpdate model, ResourceCoreBean older) {
        ResourceCoreUpdate newer = new ResourceCoreUpdate();
        newer.setWhere(model.getWhere());

        boolean changed = false;
        ResourceCoreWhere where = newer.getWhere(true);
        where.setSceneType(model.getSceneType()); // 使用场景
        where.setId(model.getId()); // 资源ID

        if (VerifyTools.isChanged(model.getKey(), older.getKey())) {
            changed = true;
            newer.setKey(model.getKey()); // 资源KEY
        }
        if (VerifyTools.isChanged(model.getName(), older.getName())) {
            changed = true;
            newer.setName(model.getName()); // 资源名称
        }
        // if (VerifyTools.isChanged(model.getType(), older.getType())) {
        //     changed = true;
        //     newer.setType(model.getType()); // 资源类型(0.模块|1.菜单组|2.菜单|3.操作)
        // }
        if (VerifyTools.isChanged(model.getUrl(), older.getUrl())) {
            changed = true;
            newer.setUrl(model.getUrl()); // 页面地址
        }
        if (VerifyTools.isChanged(model.getOptions(), older.getOptions())) {
            changed = true;
            newer.setOptions(model.getOptions()); // 选项
        }
        if (VerifyTools.isChanged(model.getSortIndex(), older.getSortIndex())) {
            changed = true;
            newer.setSortIndex(model.getSortIndex()); // 排序号(越小越靠前)
        }
        if (VerifyTools.isChanged(model.getParentId(), older.getParentId())) {
            changed = true;
            newer.setParentId(model.getParentId()); // 上级资源ID
        }
        if (VerifyTools.isChanged(model.getDescription(), older.getDescription())) {
            changed = true;
            newer.setDescription(model.getDescription()); // 描述
        }
        // if (VerifyTools.isChanged(model.getState(), older.getState())) {
        //     changed = true;
        //     newer.setState(model.getState()); // 状态(0.正常|1.禁用)
        // }
        // if (VerifyTools.isChanged(model.getDataState(), older.getDataState())) {
        //     changed = true;
        //     newer.setDataState(model.getDataState()); // 数据状态:0为正常|其他为删除
        // }
        // if (Boolean.TRUE.equals(model.isSceneTypeToNull())) {
        //     changed = true;
        //     newer.setSceneTypeToNull(true); // 使用场景更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isKeyToNull())) {
        //     changed = true;
        //     newer.setKeyToNull(true); // 资源KEY更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isNameToNull())) {
        //     changed = true;
        //     newer.setNameToNull(true); // 资源名称更新为空值
        // }
        // if (Boolean.TRUE.equals(model.isTypeToNull())) {
        //     changed = true;
        //     newer.setTypeToNull(true); // 资源类型(0.模块|1.菜单组|2.菜单|3.操作)更新为空值
        // }
        if (Boolean.TRUE.equals(model.isUrlToNull())) {
            changed = true;
            newer.setUrlToNull(true); // 页面地址更新为空值
        }
        if (Boolean.TRUE.equals(model.isOptionsToNull())) {
            changed = true;
            newer.setOptionsToNull(true); // 选项更新为空值
        }
        // if (Boolean.TRUE.equals(model.isSortIndexToNull())) {
        //     changed = true;
        //     newer.setSortIndexToNull(true); // 排序号(越小越靠前)更新为空值
        // }
        if (VerifyTools.isNotBlank(model.getSortIndexAdd()) && model.getSortIndexAdd() != 0) {
            changed = true;
            newer.setSortIndexAdd(model.getSortIndex()); // 排序号(越小越靠前)的增加值
        }
        // if (Boolean.TRUE.equals(model.isParentIdToNull())) {
        //     changed = true;
        //     newer.setParentIdToNull(true); // 上级资源ID更新为空值
        // }
        if (Boolean.TRUE.equals(model.isDescriptionToNull())) {
            changed = true;
            newer.setDescriptionToNull(true); // 描述更新为空值
        }
        // if (Boolean.TRUE.equals(model.isStateToNull())) {
        //     changed = true;
        //     newer.setStateToNull(true); // 状态(0.正常|1.禁用)更新为空值
        // }

        return changed ? newer : null;

    }

}
